package com.ztn.iot.zhonghaichi.util;

import com.ztn.base.bean.constant.ZtnWebConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

@Service
@Slf4j
public class LocalFileUtilImpl {

    public String LOCAL_FILE_PATH;

    @PostConstruct
    public void init() {
        StringBuilder fileBuilder = new StringBuilder();
        fileBuilder.append(System.getProperty("user.dir"));
        fileBuilder.append(File.separator);
        fileBuilder.append("file");
        fileBuilder.append(File.separator);
        LOCAL_FILE_PATH = fileBuilder.toString();
    }


    public int insert(String bucketName, String path, byte[] data) {
        try {
            Path p = Paths.get(LOCAL_FILE_PATH + bucketName + path);
            Files.createDirectories(p.getParent());
            Files.write(p, data);
            return 1;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return 0;
    }

}
